package com.iot.ammeter.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.IndexedColors;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName: EasyExcelExportUtils
 * @Author: Basil
 * @Date: 2022/1/20 下午2:18
 * @Version: 1.0
 * @Description: easyexcel工具类
 **/
public class EasyExcelExportUtils {
    /**
     * 注解导出工具类
     *
     * @param exportList   导出的数据
     * @param outFileName  导出的文件名称
     * @param response     响应
     * @param isCloseExcel
     * @return 文件
     */
    public static ExcelWriter exportToResponse(List exportList, String outFileName,
                                               String sheetName,
                                               Class clazz,
                                               HttpServletResponse response,
                                               boolean isCloseExcel) {
        ExcelWriter excelWriter = null;
        try {
            // 头的策略
            WriteCellStyle headWriteCellStyle = EasyExcelExportUtils.buildHeadCellStyle();
            HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle,
                    new ArrayList<WriteCellStyle>());

            excelWriter = EasyExcelExportUtils.getExcelWriter(outFileName,
                    clazz,
                    Arrays.asList(horizontalCellStyleStrategy),
                    response);

            WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).build();
            excelWriter.write(exportList, writeSheet);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (isCloseExcel) {
                // 关闭流
                if (excelWriter != null) {
                    excelWriter.finish();
                }
            }
        }
        if (isCloseExcel) {
            return null;
        } else {
            return excelWriter;
        }
    }

    /**
     * 根据输出文件名获取excelWriter类
     *
     * @param outFileName 导出的文件名称
     * @param response    响应
     */
    public static ExcelWriter getExcelWriter(String outFileName, Class clazz, List<WriteHandler> writeHandlerList, HttpServletResponse response) throws IOException {
        ExcelWriter excelWriter = null;
        response.reset();
        response.setContentType("application/octet-stream; charset=utf-8");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(outFileName, "UTF-8"));

        OutputStream out = response.getOutputStream();
        //这里指定需要表头，因为model通常包含表信头息
        if (writeHandlerList != null && !writeHandlerList.isEmpty()) {
            ExcelWriterBuilder excelWriterBuilder = EasyExcelFactory.write(out, clazz);
            for (WriteHandler writeHandler : writeHandlerList) {
                excelWriterBuilder.registerWriteHandler(writeHandler);
            }
            excelWriter = excelWriterBuilder.build();
        } else {
            excelWriter = EasyExcelFactory.write(out, clazz).build();
        }

        return excelWriter;
    }

    /**
     * Build head cell style
     *
     * @return
     */
    public static WriteCellStyle buildHeadCellStyle() {
//        CellStyle headCellStyle = StyleUtil.buildHeadCellStyle(workbook, null);
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontName("宋体");
        headWriteFont.setFontHeightInPoints((short) 12);
        headWriteFont.setBold(true);
        headWriteCellStyle.setWriteFont(headWriteFont);
        //自动换行
        headWriteCellStyle.setWrapped(false);
        return headWriteCellStyle;
    }
}
